import pymysql as db
import os

conn = db.connect(host = "localhost",
                  user = "student",
                  passwd = "anything",
                  db = "emanuelacademy")
cursor = conn.cursor()

env = os.environ.get
user = env('AUTH_USER')

cursor.execute("SELECT totalPoints, attempted, correct, streak FROM users WHERE username = '%s'" %user)
row = cursor.fetchone()
try:
    totalPoints = row[0]
    attempted = row[1]
    correct = row[2]
    streak = row[3]
except:
    totalPoints = 0
    attempted = 0
    correct = 0
    streak = 0

def addPoints(n): #Adds n points to the user score, adds one to the attempted column, correct column and streak column
    cursor.execute("UPDATE users SET totalPoints = '%d' WHERE username = '%s'" %(int(totalPoints)+n, user))
    conn.commit()
    cursor.execute("UPDATE users SET attempted = '%d', correct = '%d', streak = '%d' WHERE username = '%s'" %(int(attempted+1), int(correct+1), int(streak+1), user))
    conn.commit()
    #cursor.execute("SELECT totalPoints FROM users WHERE username = '%s'" %user)
    #row = cursor.fetchone()
    #points = row[0]
    #return points

def resetStreak(): #Resets the users streak to 0 and adds 1 to the attempted column
    cursor.execute("UPDATE users SET attempted = '%d', streak = '0' WHERE username = '%s'" %(int(attempted+1), user))
    conn.commit()
